<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload = function() {
            var user = document.getElementById('user');

            user.onblur = function() {
                var user = document.getElementById('user');
                var username = user.value;
                var pwd = document.getElementById('pwd').value;
                var xhr = null;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                var parm = "user=" + username + "&password=" + pwd;
                xhr.open('post', './test.php');
                //设置请求头(POST)
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(parm);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var span = document.getElementById('info');
                            span.innerHTML = xhr.responseText;
                        }
                    }
                }
            }
        }
    </script>
</head>

<body>
    <form action="">
        用户名: <input type="text" name="username" id="user">
        <span id="info"></span> </br>
        密码:<input type="password" name="password" id="pwd"></input>
        </br>
        <input type="submit" value="注册" id="btn">
    </form>
</body>

</html>